#include "d_Math.h"
#include <GL/gl.h>

using namespace d_Math;

d_AABB::d_AABB(d_Vec3f min, d_Vec3f max)
{
	this->min = min;
	this->max = max;
	last_test_passed = true;
}

void d_AABB::draw()
{
	d_Vec3f v000 = min;
	d_Vec3f v010 = min+d_Vec3f(max[0]-min[0],0,0);
	d_Vec3f v100 = min+d_Vec3f(0,max[1]-min[1],0);
	d_Vec3f v110 = min+d_Vec3f(max[0]-min[0], max[1]-min[1], 0);

	d_Vec3f v001 = v000+d_Vec3f(0,0,max[2]-min[2]);
	d_Vec3f v011 = v010+d_Vec3f(0,0,max[2]-min[2]);
	d_Vec3f v101 = v100+d_Vec3f(0,0,max[2]-min[2]);
	d_Vec3f v111 = v110+d_Vec3f(0,0,max[2]-min[2]);

	glDisable(GL_LIGHTING);
	//glColor3f(1,0,1);
	glBegin(GL_LINE_LOOP);
		glVertex3fv(v000.p);
		glVertex3fv(v010.p);
		glVertex3fv(v110.p);
		glVertex3fv(v100.p);
		glVertex3fv(v000.p);
		glVertex3fv(v001.p);
		glVertex3fv(v011.p);
		glVertex3fv(v111.p);
		glVertex3fv(v101.p);
		glVertex3fv(v001.p);
	glEnd();
	glBegin(GL_LINES);
		glVertex3fv(v010.p);
		glVertex3fv(v011.p);
		glVertex3fv(v110.p);
		glVertex3fv(v111.p);
		glVertex3fv(v100.p);
		glVertex3fv(v101.p);
	glEnd();
	glEnable(GL_LIGHTING);
}

d_Vec3f d_AABB::getVertexP(d_Vec3f normal)
{
	d_Vec3f p = min;
	if (normal[0] >= 0)
		p[0] = max[0];
	if (normal[1] >=0)
		p[1] = max[1];
	if (normal[2] >= 0)
		p[2] = max[2];
	return p;
}

d_Vec3f d_AABB::getVertexN(d_Vec3f normal)
{
	d_Vec3f n = max;
	if (normal[0] >= 0)
		n[0] = min[0];
	if (normal[1] >=0)
		n[1] = min[1];
	if (normal[2] >= 0)
		n[2] = min[2];
	return n;
}

